package com.winit.jumpServer.web;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.base.beans.LoginUser;
import com.winit.common.base.web.BaseController;
import com.winit.common.filter.UserInfoUtil;
import com.winit.common.util.LogUtil;
import com.winit.jumpServer.bean.PushRecordInfo;
import com.winit.jumpServer.manager.JumpServerManager;
import com.winit.jumpServer.manager.PushRecordManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 跳板机推送记录
 * Created by wangjian on 16/4/5.
 */
@RestController
@RequestMapping("/pushRecord")
public class PushRecordController extends BaseController{

    static final Logger logger = LoggerFactory.getLogger(PushRecordController.class);

    @Autowired
    private PushRecordManager pushRecordManager;
    @Autowired
    private JumpServerManager jumpServerManager;
    /**
     * 获取推送记录列表
     * @param request
     * @return
     */
    @RequestMapping("/getPushRecordList")
    @ResponseBody
    public Map<String,Object> getPushRecordList(HttpServletRequest request){
        Map<String,Object> rtnMap = new HashMap<String, Object>();
        logger.info("获取推送记录列表开始");
        LoginUser userInfo = UserInfoUtil.getUserInfo(request);
        if (userInfo == null){
            logger.info("获取推送记录列表,获取用户信息为空");
            return this.noUserRtn();
        }
        List<PushRecordInfo> list = pushRecordManager.getPushRecordList();

        rtnMap.put("code","0");
        JSONObject data = new JSONObject();
        data.put("count",list.size());
        data.put("list",list);
        rtnMap.put("data",data);
        return this.getRtnMap(rtnMap,userInfo,data);
    }

    /**
     * 创建推送
     * @param request
     * @return
     */
    @RequestMapping("/createPush")
    @ResponseBody
    public Map<String,Object> createPush(HttpServletRequest request){
        logger.info("创建推送开始");
        LoginUser userInfo = UserInfoUtil.getUserInfo(request);
        if (userInfo == null){
            logger.info("创建推送,获取用户信息为空");
            return this.noUserRtn();
        }
        String projectName = request.getParameter("projectName");
        if(StringUtils.isEmpty(projectName)){
            logger.info("创建推送,projectName为空");
            return this.paramEmptyRtn(userInfo,"projectName");
        }
        String editionNum = request.getParameter("editionNum");
        if(StringUtils.isEmpty(editionNum)){
            logger.info("创建推送,editionNum为空");
            return this.paramEmptyRtn(userInfo,"editionNum");
        }
        String testPass = request.getParameter("testPass");
        if(StringUtils.isEmpty(testPass)){
            logger.info("创建推送,testPass为空");
            return this.paramEmptyRtn(userInfo,"testPass");
        }

        Map<String,Object> rtnMap = new HashMap<>();
        try {
            boolean flag = pushRecordManager.createPush(projectName, editionNum,Boolean.parseBoolean(testPass),userInfo);
            rtnMap.put("code",flag?"0":"1");
            rtnMap.put("desc",flag?"创建推送成功":"创建推送失败");
        } catch (Exception e) {
            logger.error("com.winit.jumpServer.web.PushRecordController.createPush 异常。projectName="+projectName+",editionNum="+editionNum,e);
            rtnMap.put("code","1");
            rtnMap.put("desc","创建推送异常."+e.getMessage());
        }
        return this.getRtnMap(rtnMap,userInfo,new JSONObject());
    }

    /**
     * 重新推送
     * @param request
     * @return
     */
    @RequestMapping("/restartPush")
    @ResponseBody
    public Map<String,Object> restartPush(HttpServletRequest request){
        logger.info("重新推送开始");
        LoginUser userInfo = UserInfoUtil.getUserInfo(request);
        if (userInfo == null){
            logger.info("重新推送,获取用户信息为空");
            return this.noUserRtn();
        }
        String recordName = request.getParameter("recordName");
        if(StringUtils.isEmpty(recordName)){
            logger.info("重新推送,recordName为空");
            return this.paramEmptyRtn(userInfo,"recordName");
        }
        Map<String,Object> rtnMap = pushRecordManager.rePush(recordName,userInfo);
        return this.getRtnMap(rtnMap,userInfo,new JSONObject());
    }

    /**
     * 获取传送中推送的百分比
     * @param request
     * @return
     */
    @RequestMapping("/getPushProgress")
    @ResponseBody
    public void getPushProgress(HttpServletRequest request){
        logger.info("获取传送中推送的百分比开始");
        LoginUser userInfo = UserInfoUtil.getUserInfo(request);
        if (userInfo == null){
            logger.info("获取传送中推送的百分比,获取用户信息为空");
            return;
        }
        String recordName = request.getParameter("recordName");
        if(StringUtils.isEmpty(recordName)){
            logger.info("获取传送中推送的百分比,recordName为空");
            return;
        }

//        PushProgressTask task = new PushProgressTask(info);
//        taskExecutor.execute(task);
    }

    /**
     * 校验工程环境是否已经推送跳板机
     * @param request
     * @return
     */
    @RequestMapping("/validateProjectEnvIsPush")
    @ResponseBody
    public Map<String,Object> validateProjectEnvIsPush(HttpServletRequest request){
        logger.info("校验工程环境是否已经推送跳板机开始");
        Map<String,Object> rtnMap = new HashMap<String, Object>();
        LoginUser userInfo = UserInfoUtil.getUserInfo(request);
        if (userInfo == null){
            logger.info("校验工程环境是否已经推送跳板机,获取用户信息为空");
            return this.noUserRtn();
        }

        String projectName = request.getParameter("projectName");
        if(StringUtils.isEmpty(projectName)){
            logger.info("校验工程环境是否已经推送跳板机,projectName为空");
            return this.paramEmptyRtn(userInfo,"projectName");
        }
        String editionNum = request.getParameter("editionNum");
        if(StringUtils.isEmpty(projectName)){
            logger.info("校验工程环境是否已经推送跳板机,editionNum为空");
            return this.paramEmptyRtn(userInfo,"editionNum");
        }
        String envName = request.getParameter("envName");
        if(StringUtils.isEmpty(projectName)){
            logger.info("校验工程环境是否已经推送跳板机,envName为空");
            return this.paramEmptyRtn(userInfo,"envName");
        }
        boolean flag = jumpServerManager.validateJumpServerIsPush(projectName,editionNum,envName);
        if(flag){
            rtnMap.put("code","0");
        }else{
            rtnMap.put("code","1");
        }
        return this.getRtnMap(rtnMap,userInfo,new JSONObject());
    }

}
